<?php

namespace app\admin\controller;
use app\common\controller\BaseController;
use app\common\model\LibAdmin;
use think\response\Json;

/**
 * 后台管理员操作控制器
 * @name Admin.php
 * @package 23_zfzr2
 * @category controller
 * @link
 * @author cx
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2023-12-01
 */
class Admin extends BaseController
{
    public function shyn(){
        return json(["s"]);
    }
    /**
     * 用户登录
     */
    public function login(): Json
    {
        $data = $this->getArgsList(['username', 'password']);
        $libAdmin = new LibAdmin();
        $result = $libAdmin->userLogin($data['username'], $data['password']);
        $this->log(__CLASS__, __FUNCTION__, "用户登录", 'login');
        return json($result);
    }

    /**
     * 使用token 拉取信息
     */
    public function info(): Json
    {
        $libAdmin = new LibAdmin();
        $adminUserId = defined('ADMIN_USER_ID') ? ADMIN_USER_ID: 0;

        $adminInfo = $libAdmin->findTableData(['id' => $adminUserId]);
        if($adminInfo['errorCode'] == 0){
            if (isset($adminInfo['data']['password'])){
                unset($adminInfo['data']['password']);
            }
        }
        $this->log(__CLASS__, __FUNCTION__, "使用token 拉取信息", 'select');
        return json($adminInfo);
    }

    /**
     * 更新用户信息
     */
    public function updateInfo(): Json
    {
        $data = $this->getArgsList(['name', 'email']);
        $libAdmin = new LibAdmin();
        $result = $libAdmin->updateTableData(['id' => ADMIN_USER_ID],$data);
        $this->log(__CLASS__, __FUNCTION__, "更新用户信息", 'update');
        return json($result);
    }

    /**
     * 修改用户密码
     */
    public function modifyPassword(): Json
    {
        $libAdmin = new LibAdmin();
        $data = $this->getArgsList(['oldPassword', 'newPassword', 'newPassword2']);
        $result = $libAdmin->modifyPassword(ADMIN_USER_ID, $data['oldPassword'], $data['newPassword']);
        $this->log(__CLASS__, __FUNCTION__, "修改用户密码", 'update');
        return json($result);
    }

    /**
     * 分页渲染用户
     */
    function pagingAdmin(): Json
    {
        $page = $this->getPageInfo();
        $libAdmin = new LibAdmin();
        $searchData = $this->getArgsList(['name','date']);
        $conditions = array();
        if ($searchData['name']) {
            $conditions[] = ['username', 'like', "%{$searchData['name']}%"];
        }
        if ($searchData['date']) {
            $searchData['date'][0] = $searchData['date'][0] . " 00:00:00";
            $searchData['date'][1] = $searchData['date'][1] . " 23:59:59";
            $conditions[] = ['add_time', 'between', $searchData['date']];
        }
        $result = $libAdmin->pagingTableData($page,$conditions,'add_time desc');
        $this->log(__CLASS__, __FUNCTION__, "分页渲染用户", 'select');
        return json($result);
    }

    /**
     * 获取单个用户信息
     */
    function getAdminDetail(): Json
    {
        $id = input("id") ? input("id") : 0;
        $libAdmin = new LibAdmin();
        $result = $libAdmin->findTableData(['id' => $id]);
        if($result['errorCode'] == 0){
            if (isset($adminInfo['data']['password'])){
                unset($adminInfo['data']['password']);
            }
        }
        $this->log(__CLASS__, __FUNCTION__, "获取单个用户信息", 'select');
        return json($result);
    }

    /***
     * 添加用户
     */
    function addAdmin(): Json
    {
        $libAdmin = new LibAdmin();
        $dataInfo = $this->getArgsList(['username', 'avatar', 'email', 'newPassword2', 'name', 'password','roles']);
        $dataInfo['roles'] = "User,UserManager,UserList,UserDetail,UserTeam,BalanceList,Tinnitus,TinnitusList,TinnitusEdit,BindTinnitusQuestion,Music,MusicList,MusicAdd,MusicEdit,MusicDetail,MusicIsUse,QuestionList,QuestionAdd,QuestionEdit,QuestionDetail,QuestionDelete,QuestionIsUse,Answer,AnswerList,AnswerRecordList,SystemSetting,Account,AccountList,AccountAdd,AccountEdit,AccountDetail,AccountDelete,AccountBan,Question,Setting,AppTimesList,ListenRecordList,MusicDelete";
        $dataInfo['roles'] = explode(',', $dataInfo['roles']);
        $result = $libAdmin->addOrUpdateAdmin($dataInfo);
        $this->log(__CLASS__, __FUNCTION__, "添加用户", 'add');
        return json($result);
    }

    /***
     * 更新用户
     */
    function updateAdmin(): Json
    {
        $libAdmin = new LibAdmin();
        $dataInfo = $this->getArgsList(['username', 'id','avatar', 'email', 'newPassword2', 'name', 'password','roles']);
        $dataInfo['roles'] = "User,UserManager,UserList,UserDetail,UserTeam,BalanceList,Tinnitus,TinnitusList,TinnitusEdit,BindTinnitusQuestion,Music,MusicList,MusicAdd,MusicEdit,MusicDetail,MusicIsUse,QuestionList,QuestionAdd,QuestionEdit,QuestionDetail,QuestionDelete,QuestionIsUse,Answer,AnswerList,AnswerRecordList,SystemSetting,Account,AccountList,AccountAdd,AccountEdit,AccountDetail,AccountDelete,AccountBan,Question,Setting,AppTimesList,ListenRecordList,MusicDelete";
        $result = $libAdmin->addOrUpdateAdmin($dataInfo);
        $this->log(__CLASS__, __FUNCTION__, "更新用户", 'update');
        return json($result);
    }

    /**
     * 删除账号
     */
    function deleteAdmin(): Json
    {
        $id = input("id") ? input("id") : 0;
        $libAdmin = new LibAdmin();
        $result = $libAdmin->deleteTableData(['id' => $id],true,'avatar');
        $this->log(__CLASS__, __FUNCTION__, "删除账号", 'delete');
        return json($result);
    }

    /**
     * 禁用/启用
     */
    public function banAdmin(): Json
    {
        $dataInfo = $this->getArgsList(['id','is_ban']);
        $libAdmin = new LibAdmin();
        $result = $libAdmin->updateTableData(['id' => $dataInfo['id']],['is_ban' => $dataInfo['is_ban'] ]);
        $this->log(__CLASS__, __FUNCTION__, "禁用/启用用户", 'update');
        return json($result);
    }
}